package com.longge;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-04-21 00:47
 */
public class AddTwoNumbers {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int i = 0;
        ListNode node = new ListNode(0), cur = node;
        while (l1 != null || l2 != null) {
            int val1 = 0, val2 = 0;
            if (l1 != null) {
                val1 = l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                val2 = l2.val;
                l2 = l2.next;
            }
            int j = (val1 + val2 + i) % 10;
            i = (val1 + val2 + i) / 10;
            cur.next = new ListNode(j);
            cur = cur.next;
        }
        if (i != 0) {
            cur.next = new ListNode(i);
        }
        return node.next;
    }
}
